package com.zxn.service;

import com.zxn.utils.ElasticUtils;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;

/**
 * program: springbootelastic
 * description: and、or、 not、 in、 not_in、 range、 like、这是基本的逻辑查询，下面组合是简单实例，可相互嵌套
 *              配套restful接口在baselogic.txt中，可以对照学习。
 * author: zxn
 * create: 2019-04-06 20:25
 **/
public class BaseLogicOperationService {

    /**
     * and or not 组合
     */
    public void queryAndOrNot(){
        TransportClient client = ElasticUtils.getClient();
        QueryBuilder and = QueryBuilders.matchQuery("name","one");
        QueryBuilder or = QueryBuilders.boolQuery().should(QueryBuilders.matchQuery("age","13")).should(QueryBuilders.matchQuery("age","14"));
        QueryBuilder not = QueryBuilders.boolQuery().mustNot(QueryBuilders.matchQuery("class","java"));
        QueryBuilder qb = QueryBuilders.boolQuery().must(and).must(or).must(not);
        SearchResponse response = client.prepareSearch("base_test").setTypes("test")
                .setQuery(qb)
                .get();
        SearchHits hits = response.getHits();
        for (SearchHit hit: hits) {
            System.out.println(hit.getSource());
        }
        System.out.println(hits.totalHits);

    }

    /**
     * and in 组合
     */
    public void queryAndIn(){
        TransportClient client = ElasticUtils.getClient();
        int[] ids = {1,2,4,5};
        QueryBuilder and = QueryBuilders.matchQuery("name","one");
        QueryBuilder  in = QueryBuilders.termsQuery("id",ids);
        QueryBuilder qb = QueryBuilders.boolQuery().must(and).must(in);
        SearchResponse response = client.prepareSearch("base_test").setTypes("test")
                .setQuery(qb)
                .get();
        SearchHits hits = response.getHits();
        for (SearchHit hit: hits) {
            System.out.println(hit.getSource());
        }
        System.out.println(hits.totalHits);
    }

    /**
     * and not_in 组合
     */
    public void queryAndNotin(){
        TransportClient client = ElasticUtils.getClient();
        int[] ids = {1,2,4,5};
        QueryBuilder and = QueryBuilders.matchQuery("name","one");
        QueryBuilder  in = QueryBuilders.termsQuery("id",ids);
        QueryBuilder qb = QueryBuilders.boolQuery().must(and).mustNot(in);
        SearchResponse response = client.prepareSearch("base_test").setTypes("test")
                .setQuery(qb)
                .get();
        SearchHits hits = response.getHits();
        for (SearchHit hit: hits) {
            System.out.println(hit.getSource());
        }
        System.out.println(hits.totalHits);
    }

    /**
     * and range 组合
     */
    public void queryAndRange(){
        TransportClient client = ElasticUtils.getClient();
        QueryBuilder and = QueryBuilders.matchQuery("name","one");
        QueryBuilder  in = QueryBuilders.rangeQuery("id").gte(3).lte(6);
        QueryBuilder qb = QueryBuilders.boolQuery().must(and).must(in);
        SearchResponse response = client.prepareSearch("base_test").setTypes("test")
                .setQuery(qb)
                .get();
        SearchHits hits = response.getHits();
        for (SearchHit hit: hits) {
            System.out.println(hit.getSource());
        }
        System.out.println(hits.totalHits);
    }

    /**
     * like range 组合
     */
    public void queryLikeRange(){
        TransportClient client = ElasticUtils.getClient();
        QueryBuilder and = QueryBuilders.wildcardQuery("name","one*");
        QueryBuilder  in = QueryBuilders.rangeQuery("id").gte(3).lte(19);
        QueryBuilder qb = QueryBuilders.boolQuery().must(and).must(in);
        SearchResponse response = client.prepareSearch("base_test").setTypes("test")
                .setQuery(qb)
                .get();
        SearchHits hits = response.getHits();
        for (SearchHit hit: hits) {
            System.out.println(hit.getSource());
        }
        System.out.println(hits.totalHits);
    }

    public static void main(String[] args) {
       // 可以把方法单独放到这里执行验证
    }
}
